//
//  AliquoteVC.m
//  iTeVe
//
//  Created by Pedro Gonzalez on 12/04/14.
//  Copyright (c) 2014 SISI Software. All rights reserved.
//

#import "AliquoteVC.h"
#import "FMDBDataAccess.h"

@interface AliquoteVC ()

@end

@implementation AliquoteVC


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = NSLocalizedString(@"ANAGRAFICA_ALIQUOTE", @"Aliquote...");
    self.navigationItem.prompt = @"iTeVe";

    [self populateAliquote];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}


-(void) populateAliquote
{
    aliquote = [[NSMutableArray alloc] init];
    
    FMDBDataAccess *db = [[FMDBDataAccess alloc] init];
    
    aliquote = [db getAliquote];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [aliquote count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AliquotaCell";
    
    AliquotaCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    Aliquota *aliquota = [aliquote objectAtIndex:[indexPath row]];
    cell.codice.text = aliquota.codali;
    cell.descrizione.text = aliquota.descrizione;
    
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setPositiveFormat:@"0.00"];
    
    cell.percentuale.text = [numberFormatter stringFromNumber:aliquota.percentuale];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    Aliquota *aliquota = [aliquote objectAtIndex:[indexPath row]];
    [self.delegate selectItemViewController:self didFinishEnteringItem:aliquota]; 
    
}

@end
